using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BookLookupPlugin
{
    public partial class searchForm : Form
    {
        public searchForm()
        {
            InitializeComponent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            // Cancel and close form
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            // Clear listview
            this.listResults.Items.Clear();

            // pointer
            Cursor.Current = Cursors.WaitCursor;

            SearchResults[] results = null;

            try
            {
                // Check for search text
                if (this.textSearch.Text == null)
                {
                    throw new Exception("You must specify the text you wish to query for.");
                }

                AmazonLookup search = new AmazonLookup();

                AmazonLookup.SearchCriteria searchBy = AmazonLookup.SearchCriteria.Author;
                if (this.radioSearchAuthor.Checked)
                {
                    searchBy = AmazonLookup.SearchCriteria.Author;
                }
                else if (this.radioSearchTitle.Checked)
                {
                    searchBy = AmazonLookup.SearchCriteria.Title;
                }

                results = search.SearchForBook(searchBy, this.textSearch.Text);

                if (results != null)
                {
                    foreach (SearchResults result in results)
                    {
                        // Create listview item
                        ListViewItem itemParent = new ListViewItem(result.Title);
                        ListViewItem.ListViewSubItem itemAuthor = new ListViewItem.ListViewSubItem(itemParent, result.Author);
                        ListViewItem.ListViewSubItem itemDetailsUrl = new ListViewItem.ListViewSubItem(itemParent, result.DetailsUrl);
                        ListViewItem.ListViewSubItem itemImageUrl = new ListViewItem.ListViewSubItem(itemParent, result.ImageUrl);

                        // Add subitems to parent
                        itemParent.SubItems.Add(itemAuthor);
                        itemParent.SubItems.Add(itemDetailsUrl);
                        itemParent.SubItems.Add(itemImageUrl);

                        // Add parent to listview
                        this.listResults.Items.Add(itemParent);

                        Application.DoEvents();
                    }
                }
                // pointer
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex)
            {
                // pointer
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void listResults_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.GetSelectedSearchResult();
            this.DialogResult = DialogResult.OK;
        }

        private void listResults_MouseClick(object sender, MouseEventArgs e)
        {
            this.GetSelectedSearchResult();
            this.DialogResult = DialogResult.OK;
        }

        public ListViewItem GetSelectedSearchResult()
        {
            ListViewItem item = null;

            if (this.listResults.FocusedItem != null)
            {
                // Get selected row
                item = this.listResults.FocusedItem;

                // Set value for the property
                _ResultListViewItem = item; 
            }
            return item;
        }

        private ListViewItem _ResultListViewItem;
        public ListViewItem ResultListViewItem
        {
            get
            {
                return _ResultListViewItem;
            }
            set
            {
                _ResultListViewItem = value;
            }
        }

        private void searchForm_Load(object sender, EventArgs e)
        {
            // On load, set focus to the search textbox
            this.textSearch.Focus();
            this.textSearch.Select();
        }
    }
}